<template>
  <div class="login">
    <div class="ctimg"></div>
    <div class="loginitem">
      <div class="juxing">
        <div class="title">培训管理系统</div>
        <div class="titletext">
          <p>安全、高效、诚实、守信</p>
          <p>安全为天 质量为本！</p>
          <p style="line-height: 28px">助力能源企业智能化管理不断提升</p>
        </div>
      </div>
      <div class="loginright">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="formitem">
          <div class="text">登录账号</div>
          <el-form-item prop="username" class="userinput">
            <el-input
              class="userlogo"
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password" class="pasinput">
            <el-input
              class="paslogo"
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="请输入密码"
              @keyup.enter.native="handleLogin"
            ></el-input>
          </el-form-item>
          <el-form-item class="dengli">
            <el-button class="denglu" @click="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">平宝煤业物资设备管理系统</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
    <el-form-item prop="code">-->
    <!--        <el-input-->
    <!--          v-model="loginForm.code"-->
    <!--          auto-complete="off"-->
    <!--          placeholder="验证码"-->
    <!--          style="width: 63%"-->
    <!--          @keyup.enter.native="handleLogin"-->
    <!--        >-->
    <!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
    <!--        </el-input>-->
    <!--        <div class="login-code">-->
    <!--          <img :src="codeUrl" @click="getCode" />-->
    <!--        </div>-->
    <!--      </el-form-item>-->
    <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox> -->
    <!--  底部  -->
<!--    <div class="dibu">-->
<!--      <span>平宝公司物资管理系统</span>-->
<!--    </div>-->
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        code: [
          { required: true, trigger: "change", message: "验证码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          // if (this.loginForm.rememberMe) {
          Cookies.set("username", this.loginForm.username, { expires: 30 });
          Cookies.set("password", encrypt(this.loginForm.password), {
            expires: 30,
          });
          Cookies.set("rememberMe", this.loginForm.rememberMe, { expires: 30 });
          // } else {
          //   Cookies.remove("username");
          //   Cookies.remove("password");
          //   Cookies.remove('rememberMe');
          // }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: "/" }); //直接跳首页
              // this.$router.push({ path: "/homePage" });//调到模块主页
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/newbackgroundimage.png");
  background-size: cover;
  // position: relative;
}
.dibu {
  background: red;
  height: 40px;
  width: 100%;
}
.ctimg {
  width: 1100px;
  height: 928px;
  margin-bottom: 152px;
  background-size: cover;
  background: url("../assets/logo/ctimg.png");
  //  z-index: 1;
  //  position: relative;
  //  float: left;
  margin-right: auto;
}
.loginitem {
  z-index: 10000;
  width: 994px;
  height: 467px;
  background-color: #fff;
  //  position: relative;
  position: absolute;
  .juxing {
    position: absolute;
    margin-left: 0;
    margin-top: 0;
    width: 493px;
    height: 100%;
    background-image: url("../assets/image/juxing.png");
    background-size: cover;
    .title {
      margin-top: 132px;
      margin-left: 63px;
      font-size: 30px;
      line-height: 29px;
      color: #fff;
      padding-bottom: 39px;
      font-weight: bold;
    }
    .titletext {
      width: 262px;
      height: 71px;
      font-size: 16px;
      text-align: center;
      color: #fff;
      margin-left: 71px;
      margin-right: 100px;
      margin-top: 39px;
      margin-bottom: 156px;
      line-height: 16px;
    }
  }
  .loginright {
    height: 100%;
    width: 500px;
    right: 0;
    margin-top: 0;
    //  margin-top:61PX ;
    //  margin-left: 241PX;
    position: absolute;
    .text {
      position: absolute;
      margin-top: 61px;
      right: 241px;
      width: 91px;
      height: 24px;
      text-align: center;
      line-height: 30px;
      color: #22919c;
      font-weight: bold;
    }
    .formitem {
      height: 100%;
      width: 100%;
      position: absolute;
      top: 0px;
    }
    .userinput {
      margin: 131px auto 0 auto;
      width: 298px;
    }
    .pasinput {
      width: 298px;
      margin: 0 auto;
      margin-top: 60px;
    }
    .el-input--medium .el-input__inner {
      border: none;
      border-bottom: 1px solid #22919c;
      padding: 0 25px;
    }
    .userlogo::before {
      content: "";
      display: block;
      position: absolute;
      top: 8px;
      //  background-color:red;
      background-image: url("../assets/logo/user.png");
      width: 14px;
      height: 16px;
    }
    .paslogo::before {
      content: "";
      display: block;
      position: absolute;
      top: 8px;
      //  background-color:red;
      background-image: url("../assets/logo/password.png");
      width: 14px;
      height: 16px;
    }
    .dengli {
      //  margin-top:50px;
      line-height: 46px;
      margin: 50px auto;
      margin-left: 100px;
      .denglu {
        width: 298px;
        height: 46px;
        background-color: #22919c;
        border-radius: 5px;
        margin: 0 auto;
        text-align: center;
        color: #fff;
      }
    }
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    background: red;
    color: red;
  }
}
</style>
